//
//  SISSHouseResourceTagView.m
//  SISSAPP
//
//  Created by 优房网 on 2017/4/17.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSHouseResourceTagView.h"

@interface SISSHouseResourceTagView ()

@property (nonatomic,strong) UIView *backView;
/** containerView */
@property (weak, nonatomic) UIView *containerView;
@property (weak, nonatomic) UIView *lineView;
/** titleLabel */
@property (weak, nonatomic) UILabel *titleLabel;

@property (nonatomic, strong) NSMutableArray *stringArray;

@property (nonatomic, strong) NSMutableArray *callBackArray;
/** <#Annotation#> */
@property (strong, nonatomic) NSMutableArray *indexArray;
/** buttonArray */
@property (strong, nonatomic) NSMutableArray<UIButton *> *buttonArray;

///
@property (assign, nonatomic) BOOL isDemand;

@end
@implementation SISSHouseResourceTagView

- (instancetype)initWithFrame:(CGRect)frame isDemand:(BOOL)isDemand {
   
    self.isDemand = isDemand;
    return [self initWithFrame:frame];
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self =  [super initWithFrame:frame]) {
        
        self.buttonArray = [NSMutableArray array];
        [self setupUI];
        
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    
    if (self = [super initWithCoder:aDecoder]) {
        
        [self setupUI];
        
    }
    return self;
}

- (void)setCacheIndexArray:(NSMutableArray *)cacheIndexArray {
    
    _cacheIndexArray = cacheIndexArray;
    
    for (int i = 0; i < cacheIndexArray.count; i++) {
      UIButton *button = self.buttonArray[[cacheIndexArray[i] intValue]];
        [self buttonClickAction:button];
    }
}

/**
 设置UI
 */
- (void)setupUI {
    
    self.backgroundColor = [UIColor clearColor];
    
    _backView = [[UIView alloc] initWithFrame:self.frame];
    _backView.backgroundColor = [UIColor blackColor];
    _backView.alpha = 0.5;
    [self addSubview:_backView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
    [_backView addGestureRecognizer:tap];
    
    // 容器视图
    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT * 0.5)];
    containerView.backgroundColor = [UIColor whiteColor];
    [self addSubview:containerView];
    self.containerView = containerView;
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [containerView addSubview:cancelButton];
    
    // 顶部title
    UILabel *label = [[UILabel alloc] init];
    label.text = @"房源标签";
    label.textColor = [UIColor darkTextColor];
    label.font = [UIFont systemFontOfSize:18];
    [containerView addSubview:label];
    self.titleLabel = label;
    
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(confirmButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [containerView addSubview:confirmButton];
    
    // 线
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [containerView addSubview:lineView];
    self.lineView = lineView;
    
    // 自动布局
    CGFloat margin = 8.0;
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(containerView);
        make.top.equalTo(containerView).offset(margin);
    }];
    
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(label);
        make.left.equalTo(containerView).offset(16);
    }];
    
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(label);
        make.right.equalTo(containerView).offset(-16);
    }];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(containerView);
        make.top.equalTo(label.mas_bottom).offset(margin);
        make.height.mas_equalTo(2);
    }];
    
    
    NSArray<UIColor *> *titleCololors = @[COLOR(0x23C189), COLOR(0x33B189), COLOR(0xF76D81), COLOR(0x8E7FFF), COLOR(0x59ABFD), COLOR(0xE46B00), COLOR(0xE46BD4), COLOR(0xE46C25)];
    
    CGFloat width = SCREEN_WIDTH / 3;
    
    for (NSInteger i = 0; i < self.stringArray.count; i++) {
        // 行数
        NSInteger rows = i / 3;
        // 列数
        NSInteger cols = i % 3;
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(cols * width, 78 + 48 * rows, width, 40);
        button.tag = i;
        [button setTitle:self.stringArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:titleCololors[i] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.buttonArray addObject:button];
        
        [containerView addSubview:button];
    }
    
    [self show];
}


/**
 按钮点击事件
 */
- (void)buttonClickAction:(UIButton *)button {
    
    button.selected = !button.isSelected;
    
    if (button.isSelected) {
        
        [self.callBackArray addObject:self.stringArray[button.tag]];
        [self.indexArray addObject:@(button.tag)];
    } else {
        
        [self.callBackArray removeObject:self.stringArray[button.tag]];
        [self.indexArray removeObject:@(button.tag)];
    }
}


/**
 取消按钮点击事件
 */
- (void)cancelButtonClickAction {
    [self hide];
}

/**
 确定按钮点击事件
 */
- (void)confirmButtonClickAction {
    
    if (self.getStringArrBlock) {
        self.getStringArrBlock(self.callBackArray,self.indexArray);
    }
    
    [self hide];
}

/**
 展示
 */
- (void)show {
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    self.hidden = NO;
    _backView.alpha = 0.6;
    [UIView animateWithDuration:0.5 animations:^{
        self.containerView.frame = CGRectMake(0, SCREEN_HEIGHT * 0.5, SCREEN_WIDTH, SCREEN_HEIGHT * 0.5);
    }];
}


/**
 隐藏
 */
- (void)hide {
    
    _backView.alpha = 0;
    [UIView animateWithDuration:0.5 animations:^{
        
        self.containerView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT * 0.5);
    } completion:^(BOOL finished) {
        self.hidden = YES;
        if (self.superview) {
            [self removeFromSuperview];
        }
    }];
}

- (NSMutableArray *)callBackArray {
    
    if (!_callBackArray) {
        
        _callBackArray = [NSMutableArray array];
    }
    return _callBackArray;
}

- (NSArray *)stringArray{
    
    if (!_stringArray) {
        if (self.isDemand) {
            _stringArray = [NSMutableArray arrayWithArray:@[@"高绿化", @"电梯房", @"车位充足", @"高楼层", @"优质物业", @"地铁口", @"学位房", @"南北通透"]];
        } else {
            _stringArray = [NSMutableArray arrayWithArray:@[@"地铁口物业", @"红本在手", @"南北通透", @"满二年", @"学位房"]];
        }
       
    }
    return _stringArray;
}

- (NSMutableArray *)indexArray {
    if (!_indexArray) {
        _indexArray = [NSMutableArray array];
    }
    return _indexArray;
}

//- (NSMutableArray<UIButton *> *)buttonArray {
//    
//    if (_buttonArray) {
//        _buttonArray = [NSMutableArray array];
//    }
//    return _buttonArray;
//}

@end
